package com.zxp01.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.Set;

/*切换窗口的操作*/
public class Z05_Window {
    WebDriver driver =new ChromeDriver();
    @Test
    public void changeWindow() throws InterruptedException {
        driver.manage().window().maximize();
        driver.get("https://www.baidu.com/");
        System.out.println("第一个窗口句柄："+driver.getWindowHandle());
        Thread.sleep(2000);
        // 打开新闻后切到第二个窗口
        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();

        // 获取所有窗口的句柄,以列表的形式存储
        Set<String> handles =driver.getWindowHandles();
        System.out.println(handles);
        for(String handle:handles){
            //切换句柄
            driver.switchTo().window(handle);
            // driver.getTitle().contains("百度新闻")
            if (driver.getTitle().equals("百度新闻——海量中文资讯平台")){
                break;
            }
        }

        driver.findElement(By.id("ww")).sendKeys("测试");
        System.out.println("第二个窗口句柄："+driver.getWindowHandle());
    }
}
